package com.zts.base.resource;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zhangkh on 2017/5/31.
 */
public class MapResource extends BaseResource {

    private Map<String, Object> content = new HashMap<>();

    @SuppressWarnings("unchecked")
    @JsonAnySetter
    public void add(String key, Object value) {
        if (!content.containsKey(key)) {
            content.put(key,value);
        } else {
            Object oldValue = content.get(key);

            List values;
            if (oldValue instanceof List) {
                values = (List)oldValue;
            } else {
                values = new ArrayList();
                values.add(oldValue);
            }

            values.add(value);

            content.put(key,values);

        }

    }

    @JsonAnyGetter
    public Map<String, Object> getContent() {
        Map<String, Object> map = new HashMap<>();
        map.putAll(content);
        if (getEmbedded() != null) {
            map.put("_embedded", getEmbedded());
        }
        map.put("_links", getLinks());

        return map;
    }

}
